package com.aiti.lulian.controller.crm;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.crm.SupplyToCRMParamDto;
import com.aiti.lulian.service.SupplyToCRMService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * description: 添加商机和客户
 * auth fcb
 * date 2025-05-20 11:49
 **/
@RestController
@Api(tags = "添加商机和客户")
@RequestMapping("/crm/CustomerAndCommunicate")
public class CrmCustomerAndCommunicateInfoController extends WebController {

    @Autowired
    private SupplyToCRMService supplyToCRMService;

    @PostMapping("/add")
    @ApiOperation("添加商机和客户")
    public Message addCustomerAndCommunicateInfo(@RequestBody SupplyToCRMParamDto supplyToCRMParamDto) {
        try {
            String userId = supplyToCRMParamDto.getUserId();
            if(StrUtil.isEmpty(userId)) {
                userId = findUserId();
            }
            if(StrUtil.isEmpty(userId)) {
                return Message.fail("未获取到用户标识");
            }
            supplyToCRMService.addSupplyToCRM(supplyToCRMParamDto, userId);
            return Message.success();
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("添加商机失败");
        }
    }

    @GetMapping("/isExist")
    @ApiOperation("判断当前商机是否已存在")
    public Message isExist(SupplyToCRMParamDto supplyToCRMParamDto) {
        try {
            Integer communicateInfoCount = supplyToCRMService.isExist(supplyToCRMParamDto);
            if (communicateInfoCount > 0) {
                return Message.success(true);
            } else {
                return Message.success(false);
            }
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("添加商机失败");
        }
    }

    /**
     * 新版名片小程序-管理员后台
     */
    @PostMapping("/new-card/add")
    @ApiOperation("将用户添加至CRM")
    public Message addCRM(@RequestBody SupplyToCRMParamDto supplyToCRMParamDto) {
        try {
            String userId = supplyToCRMParamDto.getUserId();
            if (StrUtil.isEmpty(userId)) {
                userId = findUserId();
            }
            if (StrUtil.isEmpty(userId)) {
                return Message.fail("未获取到用户标识");
            }
            if (supplyToCRMParamDto.getParams() == null || supplyToCRMParamDto.getParams().isEmpty()) {
                return Message.fail("请选择数据");
            }
            return Message.success(supplyToCRMService.addCRM(supplyToCRMParamDto, userId));
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("添加失败");
        }
    }

    /**
     * 新版名片小程序-管理员后台
     */
    @GetMapping("/new-card/isCustomerExist")
    @ApiOperation("判断当前客户是否存在")
    public Message isCustomerExist(@RequestParam("companyId") String companyId, @RequestParam("name") String name) {
        try {
            return Message.success(supplyToCRMService.isCustomerExist(companyId, name));
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("查询失败");
        }
    }
}
